<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />		
		<meta name="author" content="" />
		<meta name="last-edit" content="2023.03.01" />
		<title>JS函数</title>
		<style type="text/css">

			
		</style>
    <body>
      <script type="text/javascript" src="./js/4.js"></script>
      <script type="text/javascript">
        // 1.具名函数
        // JS
        function getUserInfo(userId){
          return {
            name: '张三'
          }
        }
        function printLog(type, type2 = 1){
          console.log(type, '具名函数this:', this);
          console.log(type2);
        }
        // TS
        // function getUserInfo(userId: number){
        //   return {
        //     name: '张三'
        //   }
        // }

        (function(){
          printLog(1)
        })
        // 2.匿名函数
        setTimeout(function(){
          console.log('匿名函数this:', this);
        }, 1000)

        // 3.箭头函数
        const getOrdersInfo = (ordersId) => {
          console.log('箭头函数this:', this);
        }

        let getOrdersInfo1 = (type) => {
          console.log(type, '箭头函数this:', this);
        }

        let getOrdersInfo2 = () => { return getOrdersInfo1(1) };
        let funObj = {
          name: 'functionObject',
          fun: function(){
            console.log(3, '箭头函数this:', this);
            getOrdersInfo1(2)
          }
        }

        // this指向
        // 1.普通函数指向window
        // 2.对象内函数指向对象本身
        // 3.构造函数指向new创建实例对象
        funObj.fun()
        new printLog(2)
        // new getOrdersInfo1(2)

        // 普通函数与箭头函数区别
        // 1.写法不同
        // 2.箭头函数没有构造函数与arguments
        // 3.箭头函数没有prototype属性



        // 数组遍历方法
        let array = [
          {name: '张三', age: 22},
          {name: '李四', age: 28},
          {name: '老赵', age: 32},
          {name: '小李', age: 33},
          {name: '老刘', age: 30}]


          array.forEach((item, index) => {
            console.log(`姓名${item.name},年龄:${item.age}`);
          })

          let names = array.map((item, index) => {
            return item.name
          })
          console.log(`map遍历结果:${names}`);

          let everyResult = array.every((item, index) => {
            return item.age > 30
          })
          console.log(`every遍历结果:${everyResult}`);
          
          let someResult = array.some((item, index) => {
            return item.age > 30
          })
          console.log(`some遍历结果:${someResult}`);
          
          let filterResult = array.filter((item, index) => {
            return item.age > 30
          })
          console.log(`filter遍历结果:${filterResult}`);
          
          // reduce遍历参数:回调函数,第一次遍历回调函数第一个参数默认值
          // 回调函数参数:上次遍历返回对象,当前遍历对象,下表
          let reduceResult = array.reduce((result, current, index) => {
            current['sex'] = '男'
            result.push(current)
            return result
          }, [])
          console.log(`reduce遍历结果:`);
          reduceResult.forEach((item, index) => {
            console.log(`姓名${item.name},年龄:${item.age},性别:${item.sex}`);
          })
      </script>
    </body>
	</head>

</html>